package com.wcarmon.life.command{

    import com.adobe.cairngorm.commands.ICommand
    import com.adobe.cairngorm.control.CairngormEvent;
	import mx.controls.Alert;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.IResponder;
	import com.wcarmon.life.model.LifeModelLocator;
	import com.wcarmon.life.event.*;
	import com.wcarmon.life.business.*;
	import com.wcarmon.life.vo.*;
	import mx.utils.ObjectUtil;
	import mx.collections.*;
	import com.adobe.serialization.json.JSON;
	
	
	public class UpdateProjectComboBoxCommand implements ICommand, IResponder {
		include "../../includes/loggerSnippet.as";
	
		public function execute( event : CairngormEvent ) : void { 
			LifeModelLocator.getInstance().projectComboBoxDataIsLoaded = false;
			( new UpdateProjectComboBoxDelegate(this) ).update();
		}
	
	 	public function result( event : Object ) : void {
			var e : ResultEvent = ResultEvent(event);
			var model : LifeModelLocator = LifeModelLocator.getInstance();
			
			var projects : Array = e.result as Array; 
			var ans : Array = new Array();
			
			
			var o : Object;
			var i : int;
			for (i = 0; i < projects.length; i++) {
				o = new Object();
				o.data = projects[i].id;
				o.label = fixLength(projects[i].content);	
				o.ref_meta = projects[i].ref_meta;
				o.content = projects[i].content;
				ans[i] = o;
			}
			
			//Empty element first
			o = { data:-1, label:"", content:"", ref_meta:"" }; 
			ans.unshift(o);
			
			//log.debug( "JSON project list for combobox: \n" + JSON.encode(ans) );
			
			model.projectComboBoxArray = ans;
			model.projectComboBoxDataIsLoaded = true;
		}
	
		public function fault( event : Object ) : void {
			include "includes/FaultSnippet.as";
		}
		
		private function fixLength( s : String ) : String{
			var MAX_LENGTH : uint = 58;
			if( s.length <= MAX_LENGTH ){
				return s;
			}else{
				return s.substr(0, MAX_LENGTH-3) + '...';
			}
		}
	}
}